<template>
  <div class="live-stream-management">
    <!-- 左侧面板：直播流列表 -->
    <div class="stream-list-panel">
      <h3>所有直播流</h3>
      <ul>
        <li v-for="stream in liveStreams" :key="stream.id">
          {{ stream.name }}
          <select v-model="stream.is_active" @change="toggleStreamActive(stream)">
            <option :value="true">上架</option>
            <option :value="false">下架</option>
          </select>
          <button @click="editStream(stream)">编辑</button>
          <button @click="deleteStream(stream)">删除</button>
        </li>
      </ul>
    </div>

    <!-- 右侧面板：添加和编辑直播流 -->
    <div class="stream-form-panel">
      <!-- 添加新的直播流 -->
      <div class="add-stream">
        <h3>添加新的直播流</h3>
        <form @submit.prevent="submitNewStream">
          <input v-model="addForm.name" type="text" placeholder="直播流名称" />
          <input v-model="addForm.url" type="text" placeholder="直播流URL" />
          <button type="submit">添加</button>
        </form>
      </div>

      <!-- 编辑直播流 -->
      <div class="edit-stream" v-if="editingStream">
        <h3>编辑直播流</h3>
        <form @submit.prevent="submitStream">
          <input v-model="form.name" type="text" placeholder="直播流名称" />
          <input v-model="form.url" type="text" placeholder="直播流URL" />
          <button type="submit">更新</button>
        </form>
      </div>
    </div>
  </div>
</template>

<script>
import { getAllStreams, addLiveStream, updateLiveStreamUrl } from '@/api/live';

export default {
  data() {
    return {
      liveStreams: [],
      form: { name: '', url: '', is_active: true, is_deleted: false },
      addForm: { name: '', url: '', is_active: true },

      editingStream: null,
    };
  },

  methods: {
    async fetchLiveStreams() {
      try {
        const response = await getAllStreams();
        this.liveStreams = response.data;
      } catch (error) {
        console.error('Error fetching live streams:', error);
      }
    },

    editStream(stream) {
      this.editingStream = stream;
      this.form = { ...stream };
    },

    async toggleStreamActive(stream) {
      await this.updateStream(stream);
    },


    async deleteStream(stream) {
      const confirmation = confirm(`确定要删除 "${stream.name}" 吗？`);
      if (confirmation) {
        stream.is_deleted = true;
        await this.updateStream(stream);
      }
    },

    async updateStream(stream) {
      try {
        const response = await updateLiveStreamUrl(stream.id, stream);
        if (response.data) {
          this.fetchLiveStreams();
        } else {
          alert('操作失败');
        }
      } catch (error) {
        console.error('Error updating live stream:', error);
        alert('操作失败');
      }
    },


    async submitNewStream() {
      try {
        const response = await addLiveStream(this.addForm);
        if (response.data) {
          this.fetchLiveStreams();
          this.resetForm();
        } else {
          alert('添加直播流失败');
        }
      } catch (error) {
        console.error('Error adding new live stream:', error);
        alert('操作失败');
      }
    },

    async submitStream() {
      if (this.editingStream) {
        try {
          await this.updateStream({ ...this.editingStream, ...this.form });
          this.editingStream = null; // 清除编辑状态
          this.resetForm();
        } catch (error) {
          console.error('Error updating live stream:', error);
          alert('操作失败');
        }
      } else {
        await this.submitNewStream();
      }
    },


    resetForm() {
      this.form = { name: '', url: '', is_active: true, is_deleted: false };
      this.addForm = { name: '', url: '', is_active: true };

      this.editingStream = null;
    }
  },

  mounted() {
    this.fetchLiveStreams();
  },
};
</script>



<style scoped>
.live-stream-management {
  display: flex;
  gap: 20px;
  margin: 0 auto;
  max-width: 1200px;
  height: 85vh;
  padding: 20px;
  font-family: 'Arial', sans-serif;
}

.stream-list-panel {
  flex: 1 1 50%; /* 占据50%的宽度 */
  background-color: #f9f9f9;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 20px;
  max-height: 85vh;
  overflow-y: auto;
}

.stream-list-panel h3 {
  color: #333;
  text-align: center;
}

.stream-list-panel ul {
  list-style-type: none;
  padding: 0;
  margin: 0;
}

.stream-list-panel li {
  background-color: #fff;
  border: 1px solid #eee;
  border-radius: 4px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 5px;
  padding: 10px;
}

.stream-list-panel li select {
  border: 1px solid #ccc;
  border-radius: 4px;
  padding: 5px 10px;
  font-size: 14px;
}

.stream-list-panel button {
  background-color: #93bdd8;
  border: none;
  border-radius: 4px;
  color: white;
  cursor: pointer;
  padding: 10px 15px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 14px;
  margin-left: 5px;
}

.stream-list-panel button:hover {
  background-color: #A4D3F1;
}

.stream-form-panel {
  flex: 1 1 50%; /* 占据50%的宽度 */
  background-color: #f9f9f9;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 20px;
  display: flex;
  flex-direction: column;
}

.add-stream, .edit-stream {
  flex: 1;
  margin-bottom: 20px;
}

.stream-form-panel h3 {
  color: #333;
  text-align: center;
}

.stream-form-panel input[type="text"] {
  display: block;
  width: 100%;
  padding: 10px;
  margin: 10px 0;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
  font-size: 16px;
}

.stream-form-panel button {
  display: block;
  width: 100%;
  background-color: #93bdd8;
  color: white;
  padding: 14px 20px;
  margin: 10px 0;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 18px;
  font-weight: bold;
}

.stream-form-panel button:hover {
  background-color: #A4D3F1;
}
</style>
